<include file="Public:header" />
<div class="alert alert-info">
    菜式修改
</div>
<form class="form-horizontal" method="post" action="__URL__/update" onsubmit="return ajax_submit(this);">
    <div class="control-group">
        <label class="control-label">菜品名称<span class="require">*</span></label>
        <div class="controls">
            <input type="text" name="food_name" placeholder="长度不超过30位" maxlength="30" required value="{$data.food_name}"/>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜品类型<span class="require">*</span></label>
        <div class="controls">
            <select name="food_type" required>
                <option value="-1">--选择一个菜式类型--</option>
                <option value="本单位" <if condition="$data['food_type'] eq '本单位'">selected="selected"</if>>本单位</option>
                <option value="外来单位" <if condition="$data['food_type'] eq '外来单位'">selected="selected"</if>>外来单位</option>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label">菜品主料<span class="require">*</span></label>
        <div class="controls">
            <volist name="main_part" id="stuff">
                <div class="copy">
                    <a href="" class="btn add-main" onclick="return addItem(this)"><i class="icon-plus"></i></a>
                    <select name="main_good[]" class="main-good" onchange="getUnitAndPrice(this);">
                        <option value="-1">-- 请选择一个主料 --</option>
                        <volist name="mainGood" id="val">
                            <if condition="$stuff['food'] eq $val['id']">
                                <option value="{$val.id}" selected>{$val.stuff_name}</option>
                            <else />
                                <option value="{$val.id}">{$val.stuff_name}</option>
                            </if>
                        </volist>
                    </select>
                    &nbsp;数量：<input type="text" name="main_num[]" class="num main-num" value="{$stuff.num}" onkeyup="changeByNum(this);"/>
                    &nbsp;单位：<input type="text" class="unit main-unit" value="{$stuff.unit}" name="main_unit[]" />
                    &nbsp;单价：<input type="text" class="unit main-price" name="main_price[]" value="{$stuff.price}" onkeyup="changeByPrice(this);"/>
                    &nbsp;总价：<input type="text" class="unit main-row-count row-count" value="{$stuff['price'] * $stuff['num']}" disabled/>
                </div>
            </volist>
        </div>
        <div class="control-group">
            <label class="control-label">菜品辅料<span class="require">*</span></label>
            <div class="controls">
                <if condition="$sur_part">
                <volist name="sur_part" id="stuff">
                    <div class="copy">
                        <a href="" class="btn add-sur" onclick="return addItem(this);"><i class="icon-plus"></i></a>
                        <select name="sur_good[]" class="sur-good" onchange="getUnitAndPrice(this);">
                            <option value="-1">-- 请选择一个辅料 --</option>
                            <volist name="surGood" id="val">
                                <if condition="$stuff['food'] eq $val['id']">
                                    <option value="{$val.id}" selected>{$val.stuff_name}</option>
                                <else />
                                    <option value="{$val.id}">{$val.stuff_name}</option>
                                </if>
                            </volist>
                        </select>
                        &nbsp;数量：<input type="text" name="sur_num[]" class="num sur-num" value="{$stuff.num}" onkeyup="changeByNum(this);"/>
                        &nbsp;单位：<input type="text" class="unit sur-unit" name="sur_unit[]" value="{$stuff.unit}"/>
                        &nbsp;单价：<input type="text" class="unit sur-price" name="sur_price[]" value="{$stuff.price}" onkeyup="changeByPrice(this);"/>
                        &nbsp;总价：<input type="text" class="sur-row-count row-count" value="{$stuff['price'] * $stuff['num']}" disabled/>
                    </div>
                </volist>
                <else />
                    <div class="copy">
                        <a href="" class="btn add-sur" onclick="return addItem(this);"><i class="icon-plus"></i></a>
                        <select name="sur_good[]" class="sur-good" onchange="getUnitAndPrice(this);">
                            <option value="-1">-- 请选择一个辅料 --</option>
                            <volist name="surGood" id="val">
                                <option value="{$val.id}">{$val.stuff_name}</option>
                            </volist>
                        </select>
                        &nbsp;数量：<input type="text" name="sur_num[]" class="num sur-num" value="1" onkeyup="changeByNum(this);"/>
                        &nbsp;单位：<input type="text" class="unit sur-unit" name="sur_unit[]" />
                        &nbsp;单价：<input type="text" class="unit sur-price" name="sur_price[]" onkeyup="changeByPrice(this);"/>
                        &nbsp;总价：<input type="text" class="
                        sur-row-count row-count" disabled/>
                    </div>
                </if>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">菜式总价<span class="require">*</span></label>
            <div class="controls">
                <input type="text" class="count" disabled value="{$data.food_price_count}"/>
                <input type="hidden" class="count" name="food_price_count" value="{$data.food_price_count}"/>
            </div>
        </div>
    <div class="control-group">
        <div class="controls">
            <input type="submit" value="确　定" class="btn btn-primary" data-target="#myModal"/>
            <span class="btn btn-warning" onclick="history.back(-1);">返　回</span>
        </div>
    </div>
    <input type="hidden" name="send" value="sub"/>
    <input type="hidden" name="id" value="{$data.id}"/>
</form>

<div class="main-good" style="display:none;">
    <a href="" class="btn add-main" onclick="return addItem(this)"><i class="icon-plus"></i></a>
    <select name="main_good[]" class="main-good" onchange="getUnitAndPrice(this);">
        <option value="-1">-- 请选择一个主料 --</option>
        <volist name="mainGood" id="val">
            <option value="{$val.id}">{$val.stuff_name}</option>
        </volist>
    </select>
    &nbsp;数量：<input type="text" name="main_num[]" class="num main-num" value="1" onkeyup="changeByNum(this);"/>
    &nbsp;单位：<input type="text" class="unit main-unit" name="main_unit[]" />
    &nbsp;单价：<input type="text" class="unit main-price" name="main_price[]" onkeyup="changeByPrice(this);"/> <input type="hidden" class="main-price-temp"/>
    &nbsp;总价：<input type="text" class="unit main-row-count row-count" disabled/>
</div>

<div class="sur-good" style="display:none;">
    <a href="" class="btn add-sur" onclick="return addItem(this);"><i class="icon-plus"></i></a>
    <select name="sur_good[]" class="sur-good" onchange="getUnitAndPrice(this);">
        <option value="-1">-- 请选择一个辅料 --</option>
        <volist name="surGood" id="val">
            <option value="{$val.id}">{$val.stuff_name}</option>
        </volist>
    </select>
    &nbsp;数量：<input type="text" name="sur_num[]" class="num sur-num" value="1" onkeyup="changeByNum(this);"/>
    &nbsp;单位：<input type="text" class="unit sur-unit" name="sur_unit[]" />
    &nbsp;单价：<input type="text" class="unit sur-price" name="sur_price[]" onkeyup="changeByPrice(this);"/>
    &nbsp;总价：<input type="text" class="unit sur-row-count row-count" disabled/>
</div>

<script type="text/javascript">
    function getUnitAndPrice(_this)
    {
        var id = $(_this).val();
        var temp = $(_this).attr('class').split('-');
        var selectClass = temp[0];
        var index = $('select.'+selectClass+'-good').index($(_this));
        if(id != -1)
        {
            $.ajax({
                'url': '__URL__/getUnitAndPrice',
                'type': 'post',
                'async': false,
                'dataType': 'json',
                'data': {
                    'id': id
                },
                'success': function (result) {
                    $('input.' + selectClass + '-unit').eq(index).val(result.stuff_unit);
                    $('input.' + selectClass + '-price').eq(index).val(result.stuff_price);
                    $('input.' + selectClass + '-price-temp').eq(index).val(result.stuff_price);
                }
            });

            var price = $('input.' + selectClass + '-price').eq(index).val();
            var num = $('input.' + selectClass + '-num').eq(index).val();
            $('.' + selectClass + '-row-count').eq(index).val((num * price).toFixed(2));
        }
        else
        {
            $('input.' + selectClass + '-unit').eq(index).val('');
            $('input.' + selectClass + '-price').eq(index).val('');
            $('input.' + selectClass + '-price-temp').eq(index).val('');
            $('.' + selectClass + '-row-count').eq(index).val('');
        }

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));

    }

    function addItem(_this)
    {
        var controls = $(_this).parent().parent();
        var select = $(_this).next('select');
        var selectClass = select.attr('class');
        var copy = $('div.'+selectClass);
        var data = '<div class="copy">'+copy.html()+'</div>';
        $(controls).append(data);

        return false;
    }

    function changeByNum(_this)
    {
        if(isNaN($(_this).val()))
            $(_this).val(1);
        var temp = $(_this).prev().attr('class').split('-');
        var selectClass = temp[0];
        var index = $('.'+selectClass+'-num').index($(_this));

        var price = $('input.'+selectClass+'-price').eq(index).val();
        $('.'+selectClass+'-row-count').eq(index).val(($(_this).val() * price).toFixed(2));

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));
    }

    function changeByPrice(_this)
    {
        var temp = $(_this).prev().prev().prev().attr('class').split('-');
        var selectClass = temp[0];
        var index = $('.'+selectClass+'-price').index($(_this));

        if(isNaN($(_this).val()))
            $(_this).val($('input.'+selectClass+'-price-temp').eq(index).val());


        var num = $('input.'+selectClass+'-num').eq(index).val();
        $('.'+selectClass+'-row-count').eq(index).val(($(_this).val() * num).toFixed(2));

        var count = 0;

        $.each($('input.row-count'), function() {
            var val = Number($(this).val());
            if(val != '')
                count = count+val;
        });
        $('input.count').val(count.toFixed(2));
    }

</script>

<include file="Public:check"/>

</body>
</html>